package com.pine.app.module.security.oauth.plugin.impl.loginTGT;

import com.pine.app.module.security.core.common.AuthConstant;
import com.pine.app.module.security.core.common.CookieUtil;
import com.pine.app.module.security.core.common.enums.ErrorType;
import com.pine.app.module.security.core.common.enums.ResponseProduceType;
import com.pine.app.module.security.oauth.exception.AuthenticationException;
import com.pine.app.module.security.oauth.plugin.AbstractProcessPlugin;
import com.pine.app.module.security.oauth.plugin.Plugin;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xiaoyuan
 * @create 2020/3/6 22:35
 **/
@Slf4j
public class LoginPageTGTPlugin extends AbstractProcessPlugin {

    private OauthTgtService oauthTgtService;
//    public LoginPageTGTPlugin(OauthTgtService oauthTgtService ){
//         this.oauthTgtService = oauthTgtService;
//    }


    @Autowired
    public void setOauthTgtService(OauthTgtService oauthTgtService) {
        this.oauthTgtService = oauthTgtService;
    }

    @Override
    public void init() {
        log.info("登陆页面的票据处理插件初始化成功");
    }

    @Override
    public void prepare(HttpServletRequest request, HttpServletResponse response,Object o) throws ServletException, IOException {
       // log.info("开始执行登陆页面的票据处理插件");
        //添加一个票据   木的是异常后跳转页面后也有此票据
        request.setAttribute(AuthConstant.OAUTH_SERVER_COOKIE_KEY,oauthTgtService.createTGT());
        //获取cookie中的票据信息
        String tgcCookieValue = CookieUtil.getCookie(request, AuthConstant.OAUTH_SERVER_COOKIE_KEY);
        String error_message = "";
        //检查缓存中是否有此票据
        if (StringUtils.isEmpty(tgcCookieValue)) {
            error_message =ErrorType.ILLEGAL_REQUEST.message();
           // throw new AuthenticationException( ErrorType.BLAK_MESSAGE, ResponseProduceType.HTML,AuthConstant.DEFAULT_LOGIN_PATH);
        }else{
            OauthTgtDTO tgtDTO =  this.oauthTgtService.getOauthTgtDTO(tgcCookieValue);
            if (tgtDTO==null) {
                error_message =ErrorType.ILLEGAL_REQUEST.message();
                // throw new AuthenticationException( ErrorType.ILLEGAL_REQUEST, ResponseProduceType.HTML,AuthConstant.DEFAULT_LOGIN_PATH);
            }else{
                request.setAttribute(AuthConstant.username,tgtDTO.getLoginName());
            }
        }
        if(StringUtils.isNotBlank(error_message)){
            request.setAttribute(AuthConstant.DEFAULT_LOGIN_ERROR_KEY,error_message);
            RequestDispatcher dispatcher = request.getRequestDispatcher(AuthConstant.DEFAULT_LOGIN_PATH);
            dispatcher.forward(request, response);
        }
    }

    @Override
    public void complete(HttpServletRequest request, HttpServletResponse response,Object o) {
        //流程到此步时，那么移除掉开始生成的票据
        this.oauthTgtService.remove((String) request.getAttribute(AuthConstant.OAUTH_SERVER_COOKIE_KEY));
        request.removeAttribute(AuthConstant.OAUTH_SERVER_COOKIE_KEY);
    }

    @Override
    public int getOrder() {
        return 10;
    }

    @Override
    public boolean matcher(HttpServletRequest request,  Plugin plugin) {
        return matcher("loginPage",plugin);
    }
}
